home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Environments / AppMkr151#2 / Libraries / MPW / AMLibraryC / EventLoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-20  |  8.5 KB  |  419 lines  |  [TEXT/MPS ]

  1. /* © 1988-91, Bowers Development Corp. */
  2. /* EventLoop.c */
  3.  
  4. #include <Types.h>
  5. #include <Quickdraw.h>
  6. #include <Controls.h>
  7. #include <Desk.h>
  8. #include <Dialogs.h>
  9. #include <DiskInit.h>
  10. #include <Editions.h>
  11. #include <EPPC.h>
  12. #include <Events.h>
  13. #include <Fonts.h>
  14. #include <GestaltEqu.h>
  15. #include <Lists.h>
  16. #include <Menus.h>
  17. #include <OSEvents.h>
  18. #include <TextEdit.h>
  19. #include <ToolUtils.h>
  20. #include <Traps.h>
  21. #include <AppleEvents.h>
  22.  
  23. #include "Globals.h"
  24. #include "Cursors.h"
  25. #include "DoScrap.h"
  26. #include "Miscellany.h"
  27. #include "WindowAids.h"
  28. #include "Windowing.h"
  29. #include "Dispatcher.h"
  30. #include "MainMenu.h"
  31. #include "AEvent.h"
  32.  
  33. #include "EventLoop.h"
  34.  
  35. /*----------*/
  36. void DoMouseDown    (void);
  37. void DoMouseUp        (void);
  38. void DoKeystroke    (void);
  39. void DoActivate        (void);
  40.  
  41. /*----------*/
  42. #ifndef THINK_C
  43. extern void _DataInit (void);
  44. #endif
  45.  
  46. /*----------*/
  47. Boolean TrapAvailable    (short            tNumber,
  48.                          TrapType        tType);
  49. Boolean TrapAvailable    (short            tNumber,
  50.                          TrapType        tType)
  51. {
  52.     return (NGetTrapAddress (tNumber, tType)
  53.             != GetTrapAddress (_Unimplemented));
  54. } /*TrapAvailable*/
  55.  
  56. /*----------*/
  57. void SetStackSize    (long        stackSize);
  58. void SetStackSize    (long        stackSize)
  59. {
  60.     SetApplLimit ((Ptr) ((long) &stackSize - stackSize));
  61. } /*SetStackSize*/
  62.  
  63. /*----------*/
  64. static void    GetSysConfig    (void);
  65. static void    GetSysConfig    (void)
  66. {
  67.     OSErr            ignoreError;
  68.     long            tempLong;        /*for Gestalt*/
  69.     SysEnvRec        environs;
  70.  
  71.     sysConfig.hasGestalt = (Gestalt (gestaltVersion, &tempLong) == noErr);
  72.     if (sysConfig.hasGestalt) {
  73.         sysConfig.hasWNE = TrapAvailable (_WaitNextEvent, ToolTrap);
  74.         ignoreError = Gestalt (gestaltQuickdrawVersion, &tempLong);
  75.         sysConfig.hasColorQD = (tempLong != gestaltOriginalQD);
  76.         sysConfig.hasAppleEvents = (Gestalt (gestaltAppleEventsAttr, &tempLong) == noErr);
  77.         sysConfig.hasEditionMgr = (Gestalt (gestaltEditionMgrAttr, &tempLong) == noErr);
  78.         if (sysConfig.hasEditionMgr) {
  79.             if (!CheckOS (InitEditionPack ())) {
  80.                 sysConfig.hasEditionMgr = false;    /*unable to load Edition Manager package*/
  81.             }
  82.         }
  83.     } else {
  84.         ignoreError = SysEnvirons (curSysEnvVers, &environs);
  85.         if (environs.machineType < 0) {
  86.             sysConfig.hasWNE = false;
  87.         } else {
  88.             sysConfig.hasWNE = TrapAvailable (_WaitNextEvent, ToolTrap);
  89.         }
  90.         sysConfig.hasColorQD = environs.hasColorQD;
  91.         sysConfig.hasAppleEvents = false;
  92.         sysConfig.hasEditionMgr = false;
  93.     }
  94. } /*GetSysConfig*/
  95.  
  96. /*----------*/
  97. void Initialize        ()
  98. {
  99.  
  100. #ifndef THINK_C
  101.     UnloadSeg ((Ptr) _DataInit);
  102. #endif
  103.  
  104.     SetStackSize (15000);
  105.     MaxApplZone ();
  106.     MoreMasters ();
  107.     
  108.     InitGraf (&qd.thePort);
  109.     InitFonts ();
  110.     InitWindows ();
  111.     InitMenus ();
  112.     TEInit ();
  113.     InitDialogs (NULL);
  114.     
  115.     FlushEvents (everyEvent - diskMask - app4Mask, 0);
  116.  
  117.     GetSysConfig ();    
  118.     if (sysConfig.hasAppleEvents) {
  119.         InitializeAE ();
  120.     }
  121.     inBackground = false;
  122.     
  123.     LoadMenus ();
  124.     LoadCursors ();
  125.     InitScrap (); 
  126.     
  127.     InitGlobals ();
  128.  
  129.     InitModelessDialogs ();    
  130.     InitTitles ();        /*initialize each menu title's module*/
  131. } /*Initialize*/
  132.  
  133. /*----------*/
  134. void DoMouseDown    ()
  135. {
  136.     WindowPtr        whichWindow;
  137.     short            whichPart;
  138.  
  139.     whichPart = FindWindow (curEvent.where, &whichWindow);
  140.     switch (whichPart) {
  141.     case inDesk:
  142.             /*Do nothing*/;
  143.         break;
  144.     case inMenuBar:
  145.             DoMenu (MenuSelect (curEvent.where));
  146.         break;
  147.     case inSysWindow:
  148.             SystemClick (&curEvent, whichWindow);
  149.         break;
  150.     case inContent:
  151.             DoContent (whichWindow);
  152.         break;
  153.     case inDrag:
  154.             DoDrag (whichWindow);
  155.         break;
  156.     case inGrow:
  157.             DoGrow (whichWindow);
  158.         break;
  159.     case inGoAway:
  160.             DoGoAway (whichWindow);
  161.         break;
  162.     case inZoomIn:
  163.             DoZoom (whichWindow, inZoomIn);
  164.         break;
  165.     case inZoomOut:
  166.             DoZoom (whichWindow, inZoomOut);
  167.         break;
  168.     } /*switch*/
  169. } /*DoMouseDown*/
  170.  
  171. /*----------*/
  172. void DoMouseUp        ()
  173. {
  174.     WindowPtr        whichWindow;
  175.     short            whichPart;
  176.  
  177.     whichPart = FindWindow (curEvent.where, &whichWindow);
  178.     /*save event time to check later for double click*/
  179. } /*DoMouseUp*/
  180.  
  181. /*----------*/
  182. void DoKeystroke    ()
  183. {
  184.     short            charCode;
  185.     char            ch;
  186.  
  187.     charCode = curEvent.message & charCodeMask;
  188.     ch = charCode;
  189.     
  190.     if ((curEvent.modifiers & cmdKey) != 0) {
  191.         if (curEvent.what != autoKey) {
  192.             DoMenu (MenuKey (ch));
  193.         }
  194.     } else {
  195.         if (curWindow == NULL) {
  196.             SysBeep (1);
  197.         } else {
  198.             TypeInWindow (ch);
  199.         }
  200.     }
  201. } /*DoKeystroke*/
  202.  
  203. /*----------*/
  204. void DoUpdate    ()
  205. {
  206.     GrafPtr            savePort;
  207.     WindowPtr        saveWindow;
  208.     WindowPtr        whichWindow;
  209.  
  210.     GetPort (&savePort);
  211.     whichWindow = (WindowPtr) curEvent.message;
  212.     SetPort (whichWindow);
  213.     saveWindow = curWindow;
  214.     SetInfo (whichWindow);
  215.     BeginUpdate (whichWindow);
  216.         EraseRect (&(whichWindow->portRect));
  217.         DrawControls (whichWindow);
  218.         UpdateContent ();
  219.     EndUpdate (whichWindow);
  220.     SetInfo (saveWindow);    
  221.     SetPort (savePort);
  222. } /*DoUpdate*/
  223.  
  224. /*----------*/
  225. void DoActivate        ()
  226. {
  227.     Boolean            activate;
  228.     WindowPtr        whichWindow;
  229.  
  230.     whichWindow = (WindowPtr) curEvent.message;
  231.     SetPort (whichWindow);
  232.     SetInfo (whichWindow);
  233.     
  234.     activate = ((curEvent.modifiers & activeFlag) != 0);
  235.     if (activate) {
  236.         if ((cur->text) != NULL) {
  237.             TEActivate (cur->text);
  238.         }
  239.         ReadDeskScrap ();
  240.     }
  241.     
  242.     HiliteScroll (cur->vScroll, activate);
  243.     HiliteScroll (cur->hScroll, activate);
  244.  
  245.     ActivateContent (activate);
  246.  
  247.     if (!activate) {
  248.         WriteDeskScrap ();
  249.         if (cur->text != NULL) {
  250.             TEDeactivate (cur->text);
  251.         }
  252.         SetInfo (NULL);
  253.     }
  254. } /*DoActivate*/
  255.  
  256. /*----------*/
  257. void DoDiskEvent    ()
  258. {
  259.     #define dlgTop        75                                   
  260.     #define dlgLeft        100
  261.  
  262.     OSErr            ignore;
  263.     Point            where;
  264.  
  265.     if (HiWord (curEvent.message) != noErr) {
  266.         DILoad ();
  267.         SetPt (&where, dlgLeft, dlgTop); 
  268.         ignore = DIBadMount (where, curEvent.message);
  269.         DIUnload ();
  270.     }
  271. } /*DoDiskEvent*/
  272.  
  273. /*----------*/
  274. void DoApp4Event    ()
  275. {
  276.     #define suspendResumeMsg    1
  277.     #define resumeMask            1
  278.     
  279.     Boolean            resume;
  280.     WindowPtr        front;
  281.  
  282.     curEvent.what = nullEvent;
  283.     if (BitShift (curEvent.message, -24) == suspendResumeMsg) {
  284.         resume = ((curEvent.message & resumeMask) != 0);
  285.         if (resume) {
  286.             ReadDeskScrap ();
  287.         } else {
  288.             WriteDeskScrap ();
  289.         }
  290.         inBackground = !resume;
  291.         front = FrontWindow ();
  292.         if (front != NULL) {
  293.             curEvent.what = activateEvt;
  294.             curEvent.modifiers = LoWord (curEvent.message);
  295.             curEvent.message = (long) front;
  296.         }
  297.     }
  298. } /*DoApp4Event*/
  299.  
  300. /*----------*/
  301. long GetSleep    (void);
  302. long GetSleep    ()
  303. {
  304.     long            sleep;
  305.  
  306.     sleep = GetCaretTime ();    /* if long time, some Inits won't run */
  307.     if ((!inBackground) && (FrontWindow () != NULL)) {
  308.         if (FrontWindow () == curWindow) {
  309.             if (cur->text != NULL) {
  310.                 if ((**(cur->text)).selStart == (**(cur->text)).selEnd) {
  311.                     sleep = GetCaretTime ();
  312.                 }
  313.             }
  314.         } else {    /* DA or modeless dialog */
  315.             sleep = GetCaretTime ();
  316.         }
  317.     }
  318.     return (sleep);
  319. } /*GetSleep*/
  320.  
  321. /*----------*/
  322. static Boolean GetEvent    (void);
  323. static Boolean GetEvent    ()
  324. {
  325.     Boolean            gotEvent;
  326.  
  327.     if (sysConfig.hasWNE) {
  328.         gotEvent = WaitNextEvent (everyEvent, &curEvent,
  329.                                   GetSleep (), cursorRgn);
  330.     } else {
  331.         SystemTask ();
  332.         gotEvent = GetNextEvent (everyEvent, &curEvent);
  333.     }
  334.     return (gotEvent);
  335. } /*GetEvent*/
  336.  
  337. /*----------*/
  338. Boolean DoEvent        (void);
  339. Boolean DoEvent        ()
  340. {
  341.     Boolean            gotEvent;
  342.     DialogPtr        whichDialog;
  343.     short            itemHit;
  344.  
  345.     gotEvent = GetEvent ();
  346.     if (curEvent.what == app4Evt) {
  347.       /* here so event won't get swallowed by IsDialogEvent */
  348.       /* and so suspend/resume can be translated to activate */
  349.         DoApp4Event ();
  350.     }
  351.     if (IsDialogEvent (&curEvent)) {
  352.         if ((curEvent.what == activateEvt)
  353.         ||  (curEvent.what == updateEvt)) {
  354.             whichDialog = (DialogPtr) curEvent.message;
  355.         } else {
  356.             whichDialog = FrontWindow ();
  357.         }
  358.         SetPort (whichDialog);
  359.         if ((Boolean) FilterModeless (whichDialog, &curEvent, &itemHit)) {
  360.             DoModelessItem (whichDialog, itemHit);
  361.         } else if ((curEvent.what == keyDown)
  362.                && ((curEvent.modifiers & cmdKey) != 0)) {
  363.             DoKeystroke ();        /* => calls MenuKey */
  364.         } else if (DialogSelect (&curEvent, &whichDialog, &itemHit)) {
  365.             DoModelessItem (whichDialog, itemHit);
  366.         }
  367.     } else if (gotEvent) {
  368.         switch (curEvent.what) {
  369.         case mouseDown:
  370.                 DoMouseDown ();
  371.             break;
  372.         case mouseUp:
  373.                 DoMouseUp ();
  374.             break;
  375.         case keyDown:
  376.         case autoKey:
  377.                 DoKeystroke ();
  378.             break;
  379.         case updateEvt:
  380.                 DoUpdate ();
  381.             break;
  382.         case activateEvt:
  383.                 DoActivate ();
  384.             break;
  385.         case diskEvt:
  386.                 DoDiskEvent ();
  387.             break;
  388.         case kHighLevelEvent:
  389.                 if (sysConfig.hasAppleEvents) {
  390.                     DoHighLevelEvent ();
  391.                 }
  392.             break;
  393.         default:
  394.                 DoIdle ();
  395.             break;
  396.         } /*switch*/
  397.     }
  398.     return (gotEvent);
  399. } /*DoEvent*/
  400.  
  401. /*----------*/
  402. void MainLoop    ()
  403. {
  404.     UpdateMenus ();
  405.  
  406.     quittingTime = false;    
  407.     while (!quittingTime) {
  408.         ShapeCursor ();
  409.         if (cur->text != NULL) {
  410.             TEIdle (cur->text);
  411.         }
  412.         if (DoEvent ()) {
  413.             UpdateMenus ();
  414.         }
  415.     } /*while*/
  416.     
  417.     WriteDeskScrap ();
  418. } /*MainLoop*/
  419.